﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("StreamNo")]
    public class StreamNo : IEntity<StreamNo>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 1：学术期刊  2：非学术期刊（杂志）
        /// </summary>
        public int StreamFor { get; set; }
        /// <summary>
        /// 关联Id
        /// </summary>
        public int RelationId { get; set; }
        /// <summary>
        /// 标示，通常为yyMM
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Seed { get; set; }

        /// <summary>
        /// 序号
        /// </summary>
        public int No { get; set; }


        public override StreamNo Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override StreamNo Find(System.Linq.Expressions.Expression<Func<StreamNo, bool>> where)
        {
            using (Context ctx = new Context())
            { 
                 var item = ctx.StreamNo.FirstOrDefault(where);
                 if (item != null)
                 {
                     this.Id = item.Id;
                     this.No = item.No;
                     this.RelationId = item.RelationId;
                     this.Seed = item.Seed;
                     this.StreamFor = item.StreamFor;
                     return this;
                 }
                 return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<StreamNo, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.StreamNo.Any(where);
            }
        }
    }
}
